{layout name="common/layout" /}

<div class="dislpayArrow hidden-xs"><a class="pngfix" href="javascript:void(0);" onClick="displaynavbar(this)"></a></div>
<style>
    .main_area{
        width:100%;
        height:100%;
        position: relative;
        bottom:0;
        overflow: auto;
    }
    .post_form{
        box-sizing:unset;
        padding: 15px 50px;
        margin: -10px auto;
    }
    .red_must{
        color: red;
        padding-right: 5px;
    }
    .sub_title{
        position: relative;
        float: left;
        width: 120px;
    }
    .editer{
        margin-left: 0px;
    }
    .sub_body{

    }
    /*按钮*/
    .btn_area{
        width: 45%;
        position: relative;
        margin: 40px auto;
    }
    input {
        font-size: 14px;
        color: #222;
        margin: -5px 0 0 10px;
        outline: none;
        border: none;
    }
</style>
<div class="Hui-article-box">
    <div class="Hui-article" style="top:0px;">
        <h3 class="showlist_title">优惠券>修改优惠券</h3>

    <div class="main_area">
        <div style="position: relative;width:100%;margin-left: 60px;margin-top: 40px;">
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>类型</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input class="coupon_id" type="hidden" value="{$couponinfo['id']}" />
                    <input name="type" type="radio" style="margin-left: 0px;"  value="1" checked/>红包
                    <!--<input name="type" type="radio" style="margin:0 0 0 20px;"  value="2" />满减券-->
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>标题</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="coupon_name" type="text" class="input-text radius size-M  editer coupon_title"  value="{$couponinfo['coupon_name']}" />
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>使用范围</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    {$storeinfo['store_name']}
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>发放总量</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="number" placeholder="发放总量不能大于10万张" onkeyup="clearNoNumAndPoint(this)" type="text" class="input-text radius size-M coupon_count  editer"  value="{$couponinfo['number']}" />&nbsp;&nbsp;张
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>面值</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="amount" placeholder="面值不可高于100元" onkeyup="clearNoNum(this)" type="text" class="input-text radius size-M coupon_amount editer"  value="{$couponinfo['amount']}" />&nbsp;&nbsp;元
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>使用条件</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="limit" type="radio" style="margin-left: 0px;"  value="0" disabled/>不限制
                    <!--
                    <input name="limit" type="radio" style="margin:0 0 0 20px;"  value="1" disabled/>满
                    <input name="use_minimal_amount" type="text" class="input-text radius size-M  editer mini_amount" onkeyup="clearNoNumAndPoint(this)" style="margin-left: 10px;width:80px;"  value="{$couponinfo['use_minimal_amount']}" />&nbsp;&nbsp;元
                    -->
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>每人限领</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="user_number" type="text" class="input-text radius size-M  editer coupon_user_limit" onkeyup="clearNoNumAndPoint(this)" style="width:80px;"   value="{$couponinfo['user_number']}" />&nbsp;&nbsp;张
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>生效时间</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="effect_type" type="radio" style="margin-left: 0px;"  value="0" checked/>按领取日期
                    <input name="effect_type" type="radio" style="margin:0 0 0 20px;"  value="1" />按固定时间
                    <input name="effect_time" type="text" class="input-text radius size-M  editer Wdate effect_time" style="margin-left: 10px;"  value="{$couponinfo['effect_time']}" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'});" />
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>优惠券生效天数</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="effect_days" type="text" onkeyup="clearNoNumAndPoint(this)" class="input-text radius size-M  editer coupon_effect_days" style="width:80px;" value="{$couponinfo['effect_days']}" />&nbsp;&nbsp;天(从生效时间开始往后计算)
                </div>
            </div>
            <!--
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">&nbsp;</label>活动</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <select name="activity" class="coupon_act" style="width:200px;margin-left: 10px;">
                        <option value="0">请选择活动</option>
                        <option value="1">活动1</option>
                        <option value="2">活动2</option>
                    </select>
                </div>
            </div>
            -->
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>发放开始时间</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="activity_start_time" type="text" class="input-text radius size-M  editer Wdate coupon_start_time" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'});" value="{$couponinfo['activity_start_time']}" />
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>发放结束时间</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="activity_end_time" type="text" class="input-text radius size-M  editer Wdate coupon_end_time" onClick="WdatePicker({dateFmt:'yyyy-MM-dd'});"  value="{$couponinfo['activity_end_time']}" />
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">&nbsp;</label>规则说明链接</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="rule_intro_url" type="text" style="width:450px;" class="input-text editer radius size-M coupon_rule_url" value="{$couponinfo['rule_intro_url']}" placeholder="例如www.example.com/rule/1"/>
                </div>
            </div>
            <div class="lab cl">
                <div class="lab-l">
                    <span class="span"><label class="red_must">*</label>状态</span>
                </div>
                <div class="lab-r" style="border:0;margin-top: 10px;">
                    <input name="status" class="status" type="radio" style="margin-left: 0px;"  value="1" checked/>有效
                    <input name="status" type="radio" style="margin:0 0 0 20px;"  value="2" />失效
                </div>
            </div>
            <div  class="div-box btn_area">
                <input type="submit" class="btn add_btn" style="margin-left: 30px;" value="确定"/>
                <input type="button" class="btn close_btn" style="margin-left: 30px;" value="取消"/>
            </div>
        </div>
    </div>
</div>
</div>
<script type="text/javascript">
    $(function(){
        /*spart line*/
        //处理预数据
        var c_type = "{$couponinfo['type']}";
        if(parseInt(c_type) == 1 ){
            $("input[name='type']").eq(0).prop("checked","checked");
        }else{
            $("input[name='type']").eq(1).prop("checked","checked");
        }
        var c_use_range = "{$couponinfo['use_range']}";
        if(parseInt(c_use_range) == 1 ){
            $("input[name='use_range']").eq(0).prop("checked","checked");
        }else{
            $(".store").removeAttr("disabled");
            $(".store").css("background-color","#fff");
            $("input[name='use_range']").eq(1).prop("checked","checked");

            var c_store = "{$couponinfo['store_id']}";
            $(".store").find("option[value="+c_store+"]").attr("selected",true);
            //$(".store").val(c_store);
            //$("input[name='use_range']").eq(1).click();
        }
        var c_limit = "{$couponinfo['limit']}";
        if(parseInt(c_limit) == 0 ){
            $("input[name='limit']").eq(0).prop("checked","checked");
        }else{
            $("input[name='limit']").eq(1).prop("checked","checked");
        }
        var c_effect_type = "{$couponinfo['effect_type']}";
        if(parseInt(c_effect_type) == 0 ){
            $("input[name='effect_type']").eq(0).prop("checked","checked");
        }else{
            $("input[name='effect_type']").eq(1).prop("checked","checked");
        }
        var c_activity = "{$couponinfo['activity_id']}";
        $(".coupon_act").val(c_activity);
        var c_status = "{$couponinfo['status']}";
        if(parseInt(c_status) == 1 ){
            $("input[name='status']").eq(0).prop("checked","checked");
        }else{
            $("input[name='status']").eq(1).prop("checked","checked");
        }
        /*spart line*/
        //红包优惠券与限制的联动
        var coupontype = $("input[name='type']:checked").val();
        if(coupontype == 1){
            $("input[name='limit']").eq(0).prop("checked",'checked');
            $(".mini_amount").attr("disabled","disabled");
            $(".mini_amount").css("background-color","#f3f3f3");
        }else{
            $("input[name='limit']").eq(1).prop("checked",'checked');
            $(".mini_amount").removeAttr("disabled");
            $(".mini_amount").css("background-color","#fff");
        }
        //店铺相关
        var store_status = $("input[name='use_range']:checked").val();
        if(store_status == 1){
            $(".store").attr("disabled","disabled");
            $(".store").css("background-color","#f3f3f3");
        }else{
            $(".store").removeAttr("disabled");
            $(".store").css("background-color","#fff");
        }
        //使用门槛限制limit
        var limit = $("input[name='limit']:checked").val();
        if(limit == 0){
            $(".mini_limit").attr("disabled","disabled");
            $(".mini_limit").css("background-color","#f3f3f3");
        }else{
            $(".mini_limit").removeAttr("disabled");
            $(".mini_limit").css("background-color","#fff");
        }
        //领取生效时间相关
        var effect_type = $("input[name='effect_type']:checked").val();
        if(effect_type == 0){
            $(".effect_time").attr("disabled","disabled");
            $(".effect_time").css("background-color","#f3f3f3");
        }else{
            $(".effect_time").removeAttr("disabled");
            $(".effect_time").css("background-color","#fff");
        }
    });

    //红包优惠券与限制的联动
    $("input[name='type']").click(function(){
        if($(this).val() == 1){
            $("input[name='limit']").eq(0).prop("checked",'checked');
            $(".mini_amount").attr("disabled","disabled");
            $(".mini_amount").css("background-color","#f3f3f3");
        }else{
            $("input[name='limit']").eq(1).prop("checked",'checked');
            $(".mini_amount").removeAttr("disabled");
            $(".mini_amount").css("background-color","#fff");
        }
    });

    //领取使用范围
    $("input[name='use_range']").click(function(){
        if($(this).val() == 1){
            $(".store").attr("disabled","disabled");
            $(".store").css("background-color","#f3f3f3");
        }else{
            $(".store").removeAttr("disabled");
            $(".store").css("background-color","#fff");
        }
    });
    //使用门槛限制
    $("input[name='limit']").click(function(){
        if($(this).val() == 0){
            $(".mini_amount").attr("disabled","disabled");
            $(".mini_amount").css("background-color","#f3f3f3");
        }else{
            $(".mini_amount").removeAttr("disabled");
            $(".mini_amount").css("background-color","#fff");
        }
    });

    //领取生效时间相关
    $("input[name='effect_type']").click(function(){
        if($(this).val() == 0){
            $(".effect_time").attr("disabled","disabled");
            $(".effect_time").css("background-color","#f3f3f3");
        }else{
            $(".effect_time").removeAttr("disabled");
            $(".effect_time").css("background-color","#fff");
        }
    });

    /**
     * 数据规范化函数只能是数字
     */
    function clearNoNumAndPoint(obj){
        obj.value = obj.value.replace(/[^\d]/g,"");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        if(obj.value.indexOf(".")< 0 && obj.value !=""){
            //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value= parseFloat(obj.value);
        }
    }

    /**
     * 数据规范化函数只能是最多两位小数的数字
     */
    function clearNoNum(obj){
        obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
        if(obj.value.indexOf(".")< 0 && obj.value !=""){
            //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value= parseFloat(obj.value);
        }
    }

    /**
     * 数据提交
     */
    $(".add_btn").click(function(){
        var title = $(".coupon_title").val();
        if(title == ""){
            layer.msg("标题必填");
            return false;
        }
        var count = $(".coupon_count").val();
        if(count == ""){
            layer.msg("发放总量必填");
            return false;
        }
        var amount = $(".coupon_amount").val();
        if(amount == ""){
            layer.msg("面值必填");
            return false;
        }
        var type = parseInt($("input[name='type']:checked").val());
        if(type == 2){
            var limit = $(".mini_amount").val();
            if(limit == ""){
                layer.msg("优惠券满减条件必填");
                return false;
            }
        }
        var user_limit = $(".coupon_user_limit").val();
        if(user_limit == ""){
            layer.msg("每人限领必填");
            return false;
        }
        var  effect_type = parseInt($("input[name='effect_type']:checked").val());
        if(effect_type == 1){
            var effect_time = $(".effect_time").val();
            if(effect_time == ""){
                layer.msg("按固定时间生效时，固定时间必填");
                return false;
            }
        }
        var effect_days = $(".coupon_effect_days").val();
        if(effect_days == ""){
            layer.msg("优惠券生效天数必填");
            return false;
        }
        var coupon_start_time = $(".coupon_start_time").val();
        if(coupon_start_time == ""){
            layer.msg("发放开始时间必填");
            return false;
        }
        var coupon_end_time = $(".coupon_end_time").val();
        if(coupon_end_time == ""){
            layer.msg("发放结束时间必填");
            return false;
        }
        if($(".activity_btn").is(":checked")){
            var act_id = $(".coupon_act").val();
            var act_start = $(".coupon_start_time").val();
            var act_end = $(".coupon_end_time").val();
            if((act_start == "") || (act_end == "") || (parseInt(act_id) == 0)){
                layer.msg("活动信息不完整");
                return false;
            }
        }
        var use_range = $("input[name='use_range']:checked").val();
        if(parseInt(use_range) == 2){
            var store = $(".store").val();
            var store_name = $(".store option:selected").text();
            if(parseInt(store) == 0){
                layer.msg("请选择店铺");
                return false;
            }
        }
        var rule_intro_url = $(".coupon_rule_url").val();
        var data = {
            id: $(".coupon_id").val(),
            type: type,
            coupon_name: title,
            use_range: 2,
            amount: amount,
            number: count,
            user_number: user_limit,
            use_minimal_amount: 0,
            activity_id: 0,
            activity_name: "",
            activity_start_time: coupon_start_time,
            activity_end_time: coupon_end_time,
            effect_time: "",
            effect_days: effect_days,
            status: parseInt($("input[name='status']:checked").val()),
            store_id: "{$storeinfo['store_id']}",
            store_name: "{$storeinfo['store_name']}",
            rule_intro_url: rule_intro_url,
        }

        if(effect_type == 1){
            data.effect_time = effect_time;
        }
        if(type == 2){
            data.use_minimal_amount = limit;
        }

        $.post("{:url('coupon/updateCoupon')}",{data:data},function(data){
            if(data.status == 1){
                layer.msg(data.msg);
                window.location.href ="{:url('coupon/listCoupon')}";
            }else{
                layer.msg(data.msg);
            }
        },'json');
    });

    $(".close_btn").click(function(){
        window.location.href ="{:url('coupon/listCoupon')}";
    });
</script>